// pages/home/opendoor/opendoor.js
import Dialog from '../../../miniprogram_npm/vant-weapp/dialog/dialog';
import {
    getUserInfo
} from '../../../utils/util'
import {fetchData,resultJudge} from '../../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        message: "上午好，斯塔克",
        userDoorKeys:[],
        qRCodeInfoVo:{},
        swiperList: [],
        show:false
    },
    showPopup() {
        this.setData({ show: true });
      },
    
      onClose() {
        this.setData({ show: false });
      },
    changeMessage() {
        let message = "";
        let date = new Date()
        let hours = date.getHours()
        if (hours > 6 && hours <= 9) {
            message = "早上好," + this.data.userInfo.nickName
        }
        if (hours > 9 && hours <= 13) {
            message = "中午好," + this.data.userInfo.nickName
        }
        if (hours > 13 && hours <= 17) {
            message = "下午好," + this.data.userInfo.nickName
        }
        if (hours >= 17) {
            message = "晚上好," + this.data.userInfo.nickName
        }
        this.setData({
            message: message
        })
    },
    test(){
        Dialog.alert({
            title: '晚上好',
            message: '晚上好',
            theme: 'round-button',
          }).then(() => {
            // on close
          });
    },
    getQRCode(userId,doorKey){
    fetchData(`/base/door/getQRCode?userId=${userId}&doorKey=${doorKey}`,"","GET",res=>{
    let result= resultJudge(res,"data")
    this.setData({
        show:result.open,
        qRCodeInfoVo:result
    })
    if(!result.open){
        Dialog.alert({
            title: '扫描结果',
            message: result.message,
            theme: 'round-button',
          }).then(() => {
            // on close
          });
    }
    })
    },
    getSwiperList() {
        let that = this;
        fetchData('/base/swiper/list', {type:2}, "GET", res => {
            if (res.data.code = 200) {
                let swiperList = res.data.rows
                that.setData({
                    swiperList: swiperList
                })
                console.log(that.data.swiperList);
            }

        }, err => {

        })
    },
    openQRCode(e){
        let door_key=e.currentTarget.dataset.door_key
        this.getQRCode(this.data.userInfo.userId,door_key)
    },
    scanCodeDoor(e){
        let isEntry=e.currentTarget.dataset.isentry
        let that=this
        wx.scanCode({
            success(res) {
                let qrcode=res.result// 扫描结果
                let doorVo={
                    qrcode:qrcode,
                    approveId:that.data.userInfo.userId,
                    isEntry:isEntry
                }
                fetchData("/base/door/scanCodeDoor",doorVo,"POST",res=>{
                   let result= resultJudge(res,"data")
                   if(result.accept){
                    Dialog.alert({
                        title: '扫描结果',
                        message: result.message,
                        theme: 'round-button',
                      }).then(() => {
                        // on close
                      });
                   }else{
                    Dialog.alert({
                        title: '扫描结果',
                        message: result.message,
                        theme: 'round-button',
                      }).then(() => {
                        // on close
                      });
                   }
                })
            },
            fail(res) {
              console.log(res.errMsg); // 扫描失败的错误信息
            }
          })
       
    },
    toKeyList(){
        wx.navigateTo({
          url: './keyList/keyList',
        })
    },
    getUserDoorKeys(userId){
        fetchData(`/base/key_user/getUserDoorKeys/${userId}`,"","GET",res=>{
         let userDoorKeys=   resultJudge(res,"list")
         this.setData({
          userDoorKeys:userDoorKeys
         })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            userInfo: getUserInfo()
        })
        this.getSwiperList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.changeMessage()
        this.getUserDoorKeys(this.data.userInfo.userId)
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})